<?php
class menuModel extends Model {

    public $_validate = array(
        array('name', 'require', '菜单名称不能为空'), //必须
        array('module_name', 'require', '模块名不能为空'), //必须
        array('action_name', 'require', '方法名不能为空'), //必须
        array('pid', 'require', '上级菜单不能为空'), //必须
    );

    public function admin_menu($pid, $with_self=false) {
        $pid = intval($pid);
        $condition = array('pid' => $pid);
        if ($with_self) {
            $condition['id'] = $pid;
            $condition['_logic'] = 'OR';
        }
        $map['_complex'] = $condition;
        $map['status'] = 1;
        $menus = M("menu")->where($map)->order('ordid')->select();
        if($_SESSION['admin']['role_id']!=1){
	        $priv_mods = M('admin_auth');
	        foreach($menus as $k=>$v){
	                $r = $priv_mods->where(array('menu_id'=>$v['id'], 'role_id'=>$_SESSION['admin']['role_id']))->count();
	                if(!$r)unset($menus[$k]);
	        }
        }

        return $menus;
    }

    public function sub_menu($pid = '', $big_menu = false) {
        $array = $this->admin_menu($pid, false);
        $numbers = count($array);
        if ($numbers==1 && !$big_menu) {
            return '';
        }
        return $array;
    }

    public function get_level($id,$array=array(),$i=0) {
        foreach($array as $n=>$value){
            if ($value['id'] == $id) {
                if($value['pid']== '0') return $i;
                $i++;
                return $this->get_level($value['pid'],$array,$i);
            }
        }
    }
    
    public function get_menu_name($id) {
         $map['id'] = $id;
         $data = M("menu")->where($map)->find();
         return $data['name'];
    }
	/*
	
	*/
	public  function get_menuid($module_name,$pid){
	 	$where = "module_name='" . $module_name . "' AND pid='" . $pid . "'";
        $result = $this->where($where)->find();
        if ($result) {
            return $result['id'];
        } else {
            return false;
        }
	}
}